// 1、退出登录
$("#logout").on("click", () => {
  const isLayout = confirm("确认退出登录？");
  if (isLayout) {
    $.ajax({
      type: "post",
      url: "/logout",
      success: (res) => {
        location.href = "login.html";
      },
      error: (err) => {
        alert("服务器崩溃啦！等一下再试试吧");
      },
    });
  }
});

// 2、根据id获取用户信息，渲染左边导航栏
var id = sessionStorage.getItem("userId");

$.ajax({
  url: `/users/${id}`,
  type: "get",
  success: (res) => {
    if (res.avatar) {
      var template = `<img src="${res.avatar}" alt="">
      <h3>${res.nickName}</h3>`;
      $("#userData").html(template);
    } else {
      var template = `<img src="../uploads/default.jfif" alt="">
      <h3>${res.nickName}</h3>`;
      $("#userData").html(template);
    }
  },
});

// 3、 从地址栏获取参数的方法
function getUrlParams(name) {
  var arr = location.search.substr(1).split("&");
  for (var i = 0; i < arr.length; i++) {
    var arr2 = arr[i].split("=");

    if (arr2[0] == name) {
      return arr2[1];
    }
  }
  return false;
}
// 4、时间日期格式化
function formate(date) {
  date = new Date(date);
  return (
    date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate()
  );
}
